$(document).ready(function () {
    $('#login-username').on('input', function () {
        if ($(this).val()) {
            $('#login-username-del').attr('style', 'display: block;');
        } else {
            $('#login-username-del').attr('style', 'display: none;');
        }
    });
    $('#login-password').on('input', function () {
        if ($(this).val()) {
            $('#login-password-del').attr('style', 'display: block;');
        } else {
            $('#login-password-del').attr('style', 'display: none;');
        }
    });
    $('#login-username-del').click(function () {
        $('#login-username').val(null);
        $(this).attr('style', 'display: none;');
    });
    $('#login-password-del').click(function () {
        $('#login-password').val(null);
        $(this).attr('style', 'display: none;');
    });
    $(document).keydown(function (event_e) {
        if (window.event) {
            event_e = window.event;
        }
        var int_keycode = event_e.charCode || event_e.keyCode;
        if (int_keycode == 13) {
            $('#login-btn').click();
        }
    });
    $('#login-btn').click(function () {
        var username = $('#login-username');
        var password = $('#login-password');
        if (!username.val()) {
            layer.tips('请填写用户名', username);
            return;
        }
        if (!password.val()) {
            layer.tips('请填写密码', password);
            return;
        }
        var index = layer.msg('正在登录，请稍后...', {
            offset: [(username.offset().top+20), (username.offset().left+70)],
            shade: [0.2, '#393D49'],
            time: 0
        });
        $.ajax({
            type: 'post',
            url: '/cms/login',
            data:{
                'username': username.val(),
                'password': password.val()
                // 'password': md5(password.val())
            },
            dataType: 'json',
            success: function (data) {
                console.log(data);
                layer.close(index);
                if (data.code == 200) {
                    location.href = '/piger/main.html?userId='+data.data;
                } else {
                    layer.msg(data.msg, {
                        offset: [(username.offset().top+20), (username.offset().left+90)],
                        anim: 6
                    });
                }
            },
            error: function () {
                layer.close(index);
                layer.msg('error！', {
                    offset: [(username.offset().top+20), (username.offset().left+90)],
                    anim: 6
                });
            }
        });
    });
    $('#register-btn').click(function () {
        location.href = '/register.html';
    })
});